﻿using System;
using System.Collections.Generic;
using System.Web.Services;
using Infrastructure.Coding;
using Infrastructure.Controls;
using Infrastructure.HardCoding;
using LinQProject;
using ServiceProject.EntityService;

public partial class controls_ucRegister : System.Web.UI.UserControl
{
    public string PageToRedirect
    {
        get 
        {
            if (pageToRedirect.Value != null)
                return pageToRedirect.Value;
            return string.Empty;
        }
        set
        {
            pageToRedirect.Value = value;
        }
    }

    //protected CityService cityService = new CityService(database);
    protected RegionService regionService = new RegionService(database);
    protected UserService userService = new UserService(database);
    protected ClientService clientService = new ClientService(database);

    private static readonly FarmacieDataClassesDataContext database = new FarmacieDataClassesDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDDls();
        }
    }

    private void BindDDls()
    {
        IList<Region> listaRegion = regionService.GetAllRegion(true);
        BindEntityToControls<Region>.BindEntityListToDDL(ddlCounty, listaRegion, "RegionName", "RegionId", null);
    }

    //private void BindCity(IList<City> lista)
    //{
    //    BindEntityToControls<City>.BindEntityListToDDL(ddlCity, lista, "CityName", "CityId", null);
    //}

    protected void ddlCounty_SelectedIndexChanged(object sender, EventArgs e)
    {
        int indexCounty = Convert.ToInt32(ddlCounty.SelectedValue);
       // IList<City> listaForRegion = cityService.GetCityByRegion(indexCounty, true);
        //BindCity(listaForRegion);
        //ddlCity.Enabled = true;
    }

    [WebMethod]
    public static string GetUser()
    {
        return "Utilizator existent";
    }

    protected void lnkBtnError_Click(object sender, EventArgs e)
    {
        VerifyUser(false);
    }

    private void VerifyUser(bool saveUser)
    {
        string user = txtUser.Text;
        if (String.IsNullOrEmpty(user))
            return;

        if (userService.IsUserAvailable(user, true))
        {
            Display.DisplayMessages(lblErrorUser, Message.availableUser, true);
            if (saveUser)
                SaveLogic();
        }
        else
            Display.DisplayMessages(lblErrorUser, Message.existingUser, true);
    }

    private void SaveLogic()
    {
        User newUser = new User
                           {
                               IsActive = true,
                               IsLogged = false,
                               UserName = txtUser.Text,
                               UserPassword = new Encryption().encryptPassword(txtPassword.Text),
                           };

        Guid userId = (Guid)userService.InsertUser(newUser);
        bool isjur = rbList.SelectedValue == "0" ? true : false;
        Client newClient = new Client
                               {
                                   City= txtCity.Text,
                                   ClientAddress = txtClientAddress.Text,
                                   ClientBank = txtBank.Text,
                                   ClientCNP = txtCNP.Text,
                                   ClientCont = txtCont.Text,
                                   ClientCUI = txtCUI.Text,
                                   ClientMobil = txtMobile.Text,
                                   ClientName = txtClientName.Text,
                                   ClientPhone = txtPhone.Text,
                                   ClientReg = txtRegCom.Text,
                                   ClientEmail = txtEmail.Text,
                                   IsActive = true,
                                   RegionId = Convert.ToInt32(ddlCounty.SelectedValue),
                                   UserId = userId,
                                   RecordStamp = DateTime.Now,
                                   IsJuridical = isjur
                               };

        clientService.InsertClient(newClient);

        string DestinationUrl = !String.IsNullOrEmpty(PageToRedirect) 
                                        ? String.Format("~/Order.aspx?OrderType={0}", PageToRedirect) 
                                        : String.Format("~/FinalizeLogin.aspx");
        Response.Redirect(DestinationUrl);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        VerifyUser(true);
    }

    protected void chkIsJuridical_CheckedChanged(object sender, EventArgs e)
    {
        //bool isCheck = chkIsJuridical.Checked;
        //lblClientName.Text = isCheck ? "Denumire Societate" : "Nume si prenume";
        //lblCNP.Visible = !isCheck;
        //txtCNP.Visible = !isCheck;
        //lblCUI.Visible = isCheck;
        //txtCUI.Visible = isCheck;
        //lblRegCom.Visible = isCheck;
        //txtRegCom.Visible = isCheck;
        //chkIsJuridical.Text = isCheck ? "Persoana juridica" : "Persoana fizica";
    }

    protected void rbList_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        bool isCheck = rbList.Items[0].Selected;
        lblClientName.Text = isCheck ? "Denumire Societate" : "Nume si prenume";
        lblCNP.Visible = !isCheck;
        txtCNP.Visible = !isCheck;
        lblCUI.Visible = isCheck;
        txtCUI.Visible = isCheck;
        lblPersContact.Visible = isCheck;
        lblRegCom.Visible = isCheck;
        txtRegCom.Visible = isCheck;
        txtPersContact.Visible = isCheck;
    }
}
